//

// (  )
//

package com.tools.acceleratecore.xlib.tool.impl;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import com.tools.acceleratecore.xlib.XLibFactory;
import com.tools.acceleratecore.xlib.tool.intf.IBatteryObserver;
import com.tools.acceleratecore.xlib.tool.intf.IBatteryObserverListener;
import com.tools.acceleratecore.xlib.xlib.impl.XObserver;
import java.util.Iterator;

public class BatteryObserver extends XObserver<IBatteryObserverListener> implements IBatteryObserver {
    private Context mContext = null;
    private boolean mInited = false;
    private BroadcastReceiver mBR = null;

    public BatteryObserver() {
        this.mContext = XLibFactory.getApplication();
        this._init();
    }

    private void _init() {
        this.mBR = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                if ("android.intent.action.BATTERY_CHANGED".equals(Intent.ACTION_BATTERY_CHANGED)) {
                    synchronized(BatteryObserver.this.mListListener) {
                        Iterator var5 = BatteryObserver.this.getListListener().iterator();

                        while(var5.hasNext()) {
                            IBatteryObserverListener listener = (IBatteryObserverListener)var5.next();
                            listener.onBatteryChanged(intent);
                        }
                    }
                }

            }
        };
    }

    public boolean startListen() {
        if (this.mInited) {
            return false;
        } else {
            this.mInited = true;
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("android.intent.action.BATTERY_CHANGED");
            this.mContext.registerReceiver(this.mBR, intentFilter);
            return true;
        }
    }

    public void stopListen() {
        if (this.mInited) {
            this.mInited = false;
            this.mContext.unregisterReceiver(this.mBR);
        }
    }

    public int getBatteryLevel() {
        Intent intent = this.mContext.registerReceiver((BroadcastReceiver)null, new IntentFilter("android.intent.action.BATTERY_CHANGED"));
        int nBatteryLevel = intent.getIntExtra("level", 0);
        int nBatteryScale = intent.getIntExtra("scale", 0);
        if (nBatteryScale != 0) {
            nBatteryLevel = nBatteryLevel * 100 / nBatteryScale;
        }

        return nBatteryLevel;
    }

    public int getBatteryTemperature() {
        Intent intent = this.mContext.registerReceiver((BroadcastReceiver)null, new IntentFilter("android.intent.action.BATTERY_CHANGED"));
        return intent.getIntExtra("temperature", 0) / 10;
    }

    public int getBatteryVoltage() {
        Intent intent = this.mContext.registerReceiver((BroadcastReceiver)null, new IntentFilter("android.intent.action.BATTERY_CHANGED"));
        return intent.getIntExtra("voltage", 0);
    }

    public int getBatteryStatus() {
        Intent intent = this.mContext.registerReceiver((BroadcastReceiver)null, new IntentFilter("android.intent.action.BATTERY_CHANGED"));
        return intent.getIntExtra("status", 1);
    }

    public int getBatteryHealth() {
        Intent intent = this.mContext.registerReceiver((BroadcastReceiver)null, new IntentFilter("android.intent.action.BATTERY_CHANGED"));
        return intent.getIntExtra("health", 1);
    }
}
